var common = require("../../utils/Conmon.js");
// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    HotKeys:null,
    MyKeys:null,
    k:""
  },
  //取消
  cancel:function(e){
    wx.navigateBack();
  },
  change:function(e){
    this.data.k = e.detail.value;

  },
  //搜索
  search:function(e){
    var that=this;
    var globalData = getApp().globalData;
    var custId = globalData.custID;
   
    var k='';
    if (e.type == 'tap' && e.target.dataset.dotype==0)
    {
      k=e.target.dataset.key;
    } else{
      if (e.type == 'tap')
      {
        k = that.data.k;
      }else{
        k = e.detail.value;
      }
     
      wx.getStorage({
        key: 'HotKeys',
        success: function (res) {
          var keyList = res.data;
          if (keyList==null){
            keyList=new Array();
          }
          var has=false;
          for (var i = 0; i < keyList.length;i++)
          {
            if (keyList[i]==k)
            {
has=true;
            }
          }
          if(has==false)
          {
            if (keyList.length >= 20) {
              keyList.pop();
            }
            keyList.push(k);
            try {
              wx.setStorageSync('HotKeys', keyList)
            } catch (e) {
            }
          }
         
        }
      })
    
    } 
    var d = { "Word": k, "CustID": custId};
    var url = globalData.apiurl + '/magic/api/HotKeys/Add';

    wx.request({
      url: url,
      data: d,
      method: 'post',
      header: {
        'content-type': 'application/json',
        'X_ManuName': globalData.manuName
      },
      success: function (res) {
        wx.navigateTo({
          url: '/pages/goodsList/goodsList?keyword=' + k,
        })
      },
      fail: function () {
        wx.showToast({
          title: '操作失败',
          duration: 2000
        })
      },
      complete: function () {
        //complete
      }
    })  
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
 
    wx.getStorage({
      key: 'HotKeys',
      success: function (res) {
        var keyList = res.data;
      
        if(keyList!=null)
        {
          that.setData({
            MyKeys: keyList,

          });

         
        }else{
          
          try {
            var keyList = new Array();
            wx.setStorageSync('HotKeys', keyList);
          } catch (e) {
          }
        }
      },
      fail:function(res)
      {
       
        try {
          var keyList=new Array();
          wx.setStorageSync('HotKeys', keyList)
        } catch (e) {
        }
      }
    })
   


    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/magic/api/HotKeys/GetTopHotKeys';

    wx.request({
      url: url,
      data: { "custID": custId },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': globalData.manuName
      },
      success: function (res) {
        var hotKeys = res.data.content;
        that.setData({
          HotKeys: hotKeys,
        
        })
      },
      fail: function () {
        wx.showToast({
          title: '获取热词失败',
          duration: 2000
        })
      },
      complete: function () {
        //complete
      }
    })  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})